<?php
// Datenbank Daten holen
// Get Categories
$categoryArray = array();
include_once('inc.php');
$selectAllCategoriesQuery = "SELECT * FROM category";
$resultCategory = mysql_query($selectAllCategoriesQuery,$conn);
$num = mysql_num_rows($resultCategory);
for ($i = 0; $i < $num; $i++){
	$row = mysql_fetch_array($resultCategory);
	$tmpArrayCategory = array();
	$tmpArrayCategory['catnr'] = $row['catnr'];
	$tmpArrayCategory['catname'] = $row['catname'];
	$tmpArrayCategory['catmenu'] = $row['catmenu'];
	$categoryArray[$i] = $tmpArrayCategory;
}

// Get Questions
$questionsArray = array();
include_once('inc.php');
$selectAllQuestionsQuery = "SELECT * FROM questions";
$resultQuestion = mysql_query($selectAllQuestionsQuery,$conn);
$num = mysql_num_rows($resultQuestion);
for ($i = 0; $i < $num; $i++){
	$row = mysql_fetch_array($resultQuestion);	
	$tmpArrayQuestion = array();
	$tmpArrayQuestion['questnr'] = $row['questnr'];
	$tmpArrayQuestion['quest'] = $row['quest'];
	$questionsArray[$i] = $tmpArrayQuestion;
}

// Get Answers
$answersArray = array();
include_once('inc.php');
$selectAllAnswers = "SELECT * FROM answers";
$resultAnswers = mysql_query($selectAllAnswers,$conn);
$num = mysql_num_rows($resultAnswers);
for ($i = 0; $i < $num; $i++){
	$row = mysql_fetch_array($resultAnswers);
	$tmpArrayAnswer = array();
	$tmpArrayAnswer['questnr'] = $row['questnr'];
	$tmpArrayAnswer['answ'] = $row['answ'];
	$tmpArrayAnswer['pts'] = $row['pts'];
	$answersArray[$i] = $tmpArrayAnswer;
}

// Get QuestionCategory
$questionCategoryArray = array();
include_once('inc.php');
$selectAllQuestionCategory = "SELECT * FROM questioncategory";
$resultQuestionCategory = mysql_query($selectAllQuestionCategory,$conn);
$num = mysql_num_rows($resultQuestionCategory);
for ($i = 0; $i < $num; $i++){
	$row = mysql_fetch_array($resultQuestionCategory);
	$tmpArrayQuestionCategory = array();
	$tmpArrayQuestionCategory['questnr'] = $row['questnr'];
	$tmpArrayQuestionCategory['catnr'] = $row['catnr'];
	$questionCategoryArray[$i] = $tmpArrayQuestionCategory;
}

//Get Points
$questpoints = array();
for($i = 0; $i < count( $questionCategoryArray ); $i++ ) {
	$myQuestionNumber =  $questionCategoryArray[$i]['questnr'];
	$tmpPointsArray = array();
	$count = 0;
	for($y = 0; $y < count($answersArray); $y++) {
		if( $myQuestionNumber == $answersArray[$y]['questnr'] ) {
			$tmpPointsArray[$count] = $answersArray[$y]['pts'];
			$count++;
		}
	}
	$questpoints[$i]['Punkte'] = $tmpPointsArray;
}

$result = json_encode($questpoints);
echo $result;	
?>